<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="mt30" :size="formSize" status-icon >
        <el-form-item label="审核意见" prop="review_comment">
            <el-input v-model="ruleForm.review_comment" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
        <el-form-item class="mt30" label="查看票据" prop="pay_date">
            <el-button type="primary" @click="isShowFileMore = true"> 查看 </el-button>
            <!-- <el-button type="primary" @click="isShowFileMore = true"> 银行手续费 </el-button> -->
        </el-form-item>
        <el-form-item class="mt30" label="付款日期" prop="pay_date">
            <el-date-picker v-model="ruleForm.pay_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" />
            <span style="font-size: 14px;margin-left: 60px;">银行手续费　</span>
            <el-input-number style="width: 240px;" v-model="ruleForm.bank_commission" :precision="2"  />
        </el-form-item>
        <!-- <el-form-item label="发票类型" prop="invoice_type">
            <el-radio-group v-model="ruleForm.invoice_type">
                <el-radio value="‌增值税专用发票‌" >‌增值税专用发票‌</el-radio>
                <el-radio value="普通发票‌" >普通发票‌</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="发票编号" prop="invoice_numbers">
            <el-input v-model="ruleForm.invoice_numbers" />
        </el-form-item>
        <el-form-item label="发票" prop="invoice_file">
            <el-button type="primary" @click="onUploadFile('invoice_file')"> 上传 </el-button>
            <el-button :type="ruleForm.invoice_file ? 'primary' : 'info'" @click="onShowFile(ruleForm.invoice_file)"> 查看 </el-button>
        </el-form-item> -->
        <el-form-item label="上传回单" prop="receipt">
            <el-button type="primary" @click="onUploadFile('receipt')"> 上传 </el-button>
            <el-button :type="ruleForm.receipt ? 'primary' : 'info'" @click="onShowFile(ruleForm.receipt)"> 查看 </el-button>
        </el-form-item>
    </el-form>
    <div class="text-center">
        <el-button type="success" v-if="ruleForm.is_review == false " @click="submitForm(ruleFormRef,'approved')" :loading="loading"> 通过 </el-button>
        <el-button type="error" v-if="ruleForm.is_review == false " @click="submitForm(ruleFormRef,'rejected')" :loading="loading"> 退回 </el-button>
    </div>
    <el-divider />
    <h2 class="text-center mt30">审批流程</h2>
    <div class="p15">
        <div>发起人：{{ ruleForm.user.name }}　　发起时间：{{ ruleForm.created_at }}</div>
        <el-table :data="records" style="width: 100%">
            <el-table-column label="环节" width="100" align="center">
                <template #default="scope">
                    <el-text :type="scope.row.is_process == 1 ? 'primary' : 'info'">{{ scope.row.user.name }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="部门" width="120" align="center">
                <template #default="scope">
                    <div class="text-center"><el-text :type="scope.row.is_process == 1 ? 'primary' : 'info'" >{{ process[scope.row.level] }}</el-text></div>
                </template>
            </el-table-column>
            <el-table-column prop="status_text" width="100" label="结果" align="center" />
            <el-table-column prop="updated_at" width="180" label="时间" align="center" />
            <el-table-column prop="comment" label="意见" align="center" />
        </el-table>
    </div>

    <el-dialog v-model="showUpload" title="文件">
        <component :is="uploadFile" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>

    <el-dialog v-model="isShowFile" title="文件" width="80%" @close="onCloseFile">
        <component :is="showFile" :data="files"/>
    </el-dialog>

    <el-dialog v-model="isShowFileMore" title="文件" width="80%" @close="onCloseFile">
        <component :is="showFileArr" :data="file_arr"/>
    </el-dialog>
    
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import uploadFile from '@/components/uploadFile.vue'
import showFile from '@/components/showFile.vue'
import showFileArr from '@/components/showFileArr.vue'
import http from '../../../../util/request';
const loading = ref<boolean>(false)
const files = ref('')
const isShowFile = ref<boolean>(false)
const isShowFileMore = ref<boolean>(false)
    
const showUpload = ref<boolean>(false)
const uploadData = ref<any>({id:0,file:'',ratio:''})

const file_arr = ref<string[]>([])

interface RuleForm {
    id: number
    reviewer_id:number
    status: string
    review_comment:string
    created_at:string
    price:number
    reason:string
    payment_type:string
    pay_info:string
    pay_date:string
    invoice_numbers:string
    invoice_file:string
    bank_commission:number
    receipt:string
    invoice_type:string
    is_review:boolean
    user:any
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    reviewer_id:0,
    status: '',
    review_comment:'/',
    created_at:'',
    price:0,
    reason:'/',
    payment_type:'/',
    pay_info:'/',
    pay_date:'',
    invoice_numbers:'/',
    invoice_file:'',
    bank_commission:0,
    receipt:'',
    invoice_type:'',
    is_review:false,
    user:{id:0,name:''},
})

const process = ref<string[]>(['','项目部','预算员','综合管理部','项目合伙人','总经理','财务','出纳'])

const records: any = ref([])

const onGetData = () => {
    if(!ruleForm.value.id){
        return
    }
    http.get('/approve/'+ruleForm.value.id).then((response:any)=>{
        ruleForm.value = response.data.data
        records.value = response.data.data.records
        file_arr.value = response.data.file_arr
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.reviewer_id = 0
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    status: [
        { required: true, message: '请选择操作类型', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined, status: string) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            ruleForm.value.status = status
            loading.value = true
            let obj:any = {
                application_id:ruleForm.value.id,
                comment:ruleForm.value.review_comment,
                status:ruleForm.value.status,
                invoice_numbers:ruleForm.value.invoice_numbers,
                invoice_file:ruleForm.value.invoice_file,
                receipt:ruleForm.value.receipt,
                invoice_type:ruleForm.value.invoice_type,
                pay_date:ruleForm.value.pay_date
            }
            http.patch('/approve/review',obj).then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}


const onShowFile = (file:string) => {
    files.value = file
    isShowFile.value = true
}

const onCloseFile = () => {
    files.value = ''
}


const onUploadFile = (type:string) => {
    uploadData.value.id = ruleForm.value.id
    uploadData.value.type = type
    if(type == 'invoice_file'){
        uploadData.value.file = ruleForm.value.invoice_file
    }

    if(type == 'receipt'){
        uploadData.value.file = ruleForm.value.receipt
    }
    showUpload.value = true
}

const saveUpload = (data:string) => {
    if(uploadData.value.type == 'invoice_file'){
        ruleForm.value.invoice_file = data
    }

    if(uploadData.value.type == 'receipt'){
        ruleForm.value.receipt = data
    }
    showUpload.value = false
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
